# -*- coding: utf-8 -*-
# 导入必要的库
import numpy as np
import pandas as pd
from BPNN import BPNNRegression
import os
from sklearn import preprocessing
from parser import args
from logger import *


# logger = logger1()

def dataset(logger):
    # 参数设置
    logger.info(f"==============数据加载================")
    df1 = pd.read_csv(args.load_path)  # 数据读取
    logger.info(f'数据集加载路径:, {args.load_path}')
    split_ratio = args.split_ratio  # 训练测试比例
    # save_model_name = "bp4.pkl"  # 保存模型的名称
    save_model_path = args.save_model_path  # 保存模型的路径

    if not os.path.exists(save_model_path):
        with open(save_model_path, mode='w', encoding='utf-8'):
            print("文件创建成功！")
    # 数据集截取
    df1 = df1.iloc[:, :]
    df2 = df1.iloc[:, -1:]
    # 进行数据归一化
    min_max_scaler = preprocessing.MinMaxScaler()
    df0 = min_max_scaler.fit_transform(df1)
    df = pd.DataFrame(df0, columns=df1.columns)
    df1 = pd.DataFrame(df0, columns=df1.columns)
    features = args.features
    # x = df1.iloc[:, 0:-1]
    # y = df1.iloc[:, -1:]
    x = df1.iloc[:, features]
    y = df1.iloc[:, -1:]
    logger.info(f'features: {args.features}')
    logger.info(f'feature.shape: {x.shape}')
    logger.info(f'target.shape: {y.shape}')

    # 划分训练集测试集
    cut = args.cut
    logger.info(f'cut: {0 - cut}')
    x_train, x_test = x.iloc[:-cut], x.iloc[-cut:]  # 训练集和测试集 feature的划分
    y_train, y_test = y.iloc[:-cut], y.iloc[-cut:]  # 训练集和测试集 target的划分
    x_train, x_test = x_train.values, x_test.values
    y_train, y_test = y_train.values, y_test.values
    logger.info(f'train_feature.shape: {x_train.shape}')
    logger.info(f'test_feature.shape: {x_train.shape}')
    # 神经网络搭建
    input_size = args.input_size
    bp1 = BPNNRegression([input_size, 16, 1])
    train_data = [[sx.reshape(input_size, 1), sy.reshape(1, 1)] for sx, sy in zip(x_train, y_train)]
    test_data = [np.reshape(sx, (input_size, 1)) for sx in x_test]

    return train_data, test_data, bp1, save_model_path, y_test, df2
